package org.de.maeyer.statistic.appender.swt;

import static org.de.maeyer.unit.UnitUtils.*;

import org.de.maeyer.sampler.ConcurrentSamplerManager;
import org.de.maeyer.sampler.ObjectSampler;
import org.de.maeyer.sampler.SamplerManager;
import org.de.maeyer.statistic.view.SamplerMonitor;
import org.de.maeyer.statistic.view.View;
import org.de.maeyer.statistic.view.ViewConfiguration;
import org.de.maeyer.statistic.view.ViewConfigurationImpl;
import org.de.maeyer.statistic.view.swt.BarChart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author Peter De Maeyer
 */
public class SamplerMonitorApp {

	public static void main(String[] args) {
		Display display = new Display();

		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Pie Chart");
		shell.setBounds(10, 10, 200, 200);
		shell.setSize(400, 350);
		shell.open();
		
		SamplerManager samplerManager = new ConcurrentSamplerManager();
		ObjectSampler sampler = samplerManager.newObjectSampler("test.statistic", VOLT).accumulateHistogram();
		ViewConfiguration config = new ViewConfigurationImpl(display);
		View view = new BarChart(shell, config);
//		View view = new PieChart(shell, config);
		SamplerMonitor monitor = new SamplerMonitor(shell);
		monitor.registerView(sampler, view);
		monitor.start();

		Shell input = new Shell(display);
		input.setLayout(new GridLayout());
		input.setText("Input");
		input.setBounds(410, 20, 200, 200);
		input.setSize(400, 350);
		newButton(input, sampler, "Apple");
		newButton(input, sampler, "Pear");
		newButton(input, sampler, "Champagne");
		newButton(input, sampler, "Raccoon");
		newButton(input, sampler, "Chair");
		newButton(input, sampler, "Pillow");
		newButton(input, sampler, "Keyboard");
		newButton(input, sampler, "Soap");
		input.open();
		while (!shell.isDisposed() || !input.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
		monitor.interrupt();
	}

	private static void newButton(Shell input, final ObjectSampler sampler, final Object category) {
		Button calculator = new Button(input, SWT.PUSH);
		calculator.setText(String.valueOf(category));
		calculator.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent event) {
				sampler.sample(category);
			}
		});
	}
}
